const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const ImageGenerationTask = sequelize.define('ImageGenerationTask', {
    task_id: {
        type: DataTypes.CHAR(36),
        primaryKey: true,
        allowNull: false,
        comment: '任务唯一标识符(UUID)'
    },
    user_uuid: {
        type: DataTypes.CHAR(36),
        allowNull: false,
        comment: '用户UUID'
    },
    ai_person_id: {
        type: DataTypes.INTEGER,
        allowNull: true,
        comment: '关联的AI角色ID'
    },
    status: {
        type: DataTypes.STRING(50),
        allowNull: false,
        defaultValue: 'pending',
        comment: '任务状态'
    },
    priority: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 5,
        comment: '任务优先级'
    },
    character_trigger: {
        type: DataTypes.TEXT,
        allowNull: false,
        comment: '角色触发词'
    },
    user_prompt: {
        type: DataTypes.TEXT,
        allowNull: false,
        comment: '用户原始提示词'
    },
    refined_prompt: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '提炼后的英文提示词'
    },
    comfyui_prompt_id: {
        type: DataTypes.STRING(255),
        allowNull: true,
        comment: 'ComfyUI任务ID'
    },
    comfyui_workflow: {
        type: DataTypes.TEXT('long'),
        allowNull: true,
        comment: '工作流JSON'
    },
    image_width: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 720,
        comment: '图像宽度'
    },
    image_height: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 1280,
        comment: '图像高度'
    },
    sampling_steps: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 25,
        comment: '采样步数'
    },
    image_filename: {
        type: DataTypes.STRING(255),
        allowNull: true,
        comment: '生成的图像文件名'
    },
    image_url: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '图像访问URL'
    },
    image_size_bytes: {
        type: DataTypes.INTEGER,
        allowNull: true,
        comment: '图像文件大小'
    },
    started_at: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '开始处理时间'
    },
    prompt_refined_at: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '提示词提炼完成时间'
    },
    comfyui_submitted_at: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '提交到ComfyUI时间'
    },
    completed_at: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '完成时间'
    },
    error_message: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '错误信息'
    },
    retry_count: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '重试次数'
    },
    max_retries: {
        type: DataTypes.INTEGER,
        allowNull: false,
        defaultValue: 3,
        comment: '最大重试次数'
    },
    total_processing_time: {
        type: DataTypes.FLOAT,
        allowNull: true,
        comment: '总处理时间（秒）'
    },
    prompt_refining_time: {
        type: DataTypes.FLOAT,
        allowNull: true,
        comment: '提示词提炼耗时（秒）'
    },
    comfyui_processing_time: {
        type: DataTypes.FLOAT,
        allowNull: true,
        comment: 'ComfyUI处理耗时（秒）'
    },
    client_ip: {
        type: DataTypes.STRING(45),
        allowNull: true,
        comment: '客户端IP地址'
    },
    user_agent: {
        type: DataTypes.TEXT,
        allowNull: true,
        comment: '用户代理字符串'
    }
}, {
    tableName: 'image_generation_tasks',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '图像生成任务表',
    indexes: [
        {
            name: 'idx_user_uuid',
            fields: ['user_uuid']
        },
        {
            name: 'idx_status',
            fields: ['status']
        },
        {
            name: 'idx_created_at',
            fields: ['created_at']
        },
        {
            name: 'idx_user_status_created',
            fields: ['user_uuid', 'status', { attribute: 'created_at', order: 'DESC' }]
        }
    ]
});

// 定义与 Visitor 的关联关系
ImageGenerationTask.associate = function(models) {
    ImageGenerationTask.belongsTo(models.Visitor, {
        foreignKey: 'user_uuid',
        targetKey: 'uuid',
        as: 'visitor'
    });
};

module.exports = ImageGenerationTask; 